package Unit

import (
	. "GameLogic/GameConfig"
	. "GameLogic/Scene"
)

type Weapon struct {
	Name         string
	AttackSpeed  float64
	AttackEffect IEffect
	AttackCD     float64
	OwnerUnit    *Unit

	BeforeAttackRoll float64

	CurBfeAtkRoll float64
	IsAttacking   bool
}

func NewWeapon(xmlWeapon *XmlWeapon) *Weapon {
	weapon := &Weapon{}
	weapon.Name = xmlWeapon.Name
	weapon.AttackSpeed = xmlWeapon.AttackSpeed
	weapon.AttackCD = weapon.AttackSpeed
	weapon.BeforeAttackRoll = xmlWeapon.BeforeAttackRoll
	if xmlWeapon != nil {
		weapon.AttackEffect = CreateEffect(xmlWeapon.EffectName)
	}
	weapon.AttackEffect.GetBaseInfo().SrcUnit = weapon.OwnerUnit
	return weapon
}

func (this *Weapon) Init(unit *Unit) {
	this.OwnerUnit = unit
}

func (this *Weapon) ExecAttack(unit IUnit) {

	this.CurBfeAtkRoll = this.BeforeAttackRoll
	this.IsAttacking = true
	if this.AttackEffect != nil {
		this.AttackEffect.GetBaseInfo().DstUnit = unit
	}
}

func (this *Weapon) Update() {
	if this.IsAttacking {
		this.CurBfeAtkRoll -= 0.016
		if this.CurBfeAtkRoll <= 0 {
			if this.AttackEffect != nil {
				this.AttackEffect.Exec()
			}
			this.IsAttacking = false
			this.CurBfeAtkRoll = 0
		}
	}
}
